package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.ProvideSamples;
import cn.openea.perpartner.meily.web.entity.ProvideSamplesSub;
import cn.openea.perpartner.meily.web.entity.dto.ProvideSamplesSubDTOFactory;
import cn.openea.perpartner.meily.web.entity.vo.ProvideSamplesSubVo;
import cn.openea.perpartner.meily.web.service.IProvideSamplesSubService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ysk
 * @email zane_ysk@qq.com
 * @date 2023-03-27 11:37:04
 */
@Api(tags = "样品领取")
@RestController
@RequestMapping("/provide/samples/sub")
public class ProvideSamplesSubController extends BaseController {

    @Resource
    private IProvideSamplesSubService provideSamplesSubService;

    @Resource
    private ProvideSamplesSubDTOFactory provideSamplesSubDTOFactory;

    @ApiOperation(value = "校验是否可以报名")
    @PostMapping("/verifyRegistration")
    public AjaxResult verifyRegistration(@RequestBody @Valid ProvideSamplesSub samplesSub) {
        JSONObject object = this.provideSamplesSubService.verifyRegistration(samplesSub);
        if (object.getInteger("code") != 200) {
            return AjaxResult.error(object.getInteger("code"), object.getString("msg"));
        }
        return AjaxResult.success("可以报名！");
    }

    @ApiOperation(value = "锁定名额")
    @PostMapping("/lockedQuota")
    public AjaxResult lockedQuota(@RequestBody @Valid ProvideSamplesSub samplesSub) {
        return this.provideSamplesSubService.lockedQuota(samplesSub);
    }

    @ApiOperation(value = "领取样品")
    @PostMapping("/receive")
    public AjaxResult receive(@RequestBody @Valid ProvideSamplesSub samplesSub) {
        provideSamplesSubService.receiveProvideSamples(samplesSub);
        return AjaxResult.success("领取成功！", samplesSub);
    }

    @ApiOperation(value = "回收名额")
    @PostMapping("/recyclingQuota")
    public AjaxResult recyclingQuota(@RequestBody ProvideSamplesSubVo samplesSubVo) {
        return this.provideSamplesSubService.recyclingQuota(samplesSubVo);
    }

    @ApiOperation(value = "获取领取样品记录详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) throws ParseException {
        return AjaxResult.success("获取领取样品记录详情成功！", provideSamplesSubService.getReceiveDetails(id));
    }

    @ApiOperation(value = "获取领取样品记录列表")
    @PostMapping("/getListByMainId")
    public TableDataInfo getListByMainId(@RequestBody ProvideSamplesSubVo vo) {
        startPage();
        List<ProvideSamplesSub> list = provideSamplesSubService.getListByVo(vo);
        return getDataTable(list);
    }

    @ApiOperation(value = "订单确认收货")
    @PostMapping("/confirmReceipt")
    public AjaxResult receipt(@RequestBody ProvideSamplesSubVo vo) throws ParseException {
        ProvideSamplesSub provideSamplesSub = provideSamplesSubService.getById(vo.getId());
        if (provideSamplesSub == null) {
            return AjaxResult.success("订单不存在");
        }
        if ("待发货".equals(provideSamplesSub.getOrderStatus())) {
            return AjaxResult.success("订单还未发货");
        } else if ("已完成".equals(provideSamplesSub.getOrderStatus())) {
            return AjaxResult.success("订单已完成");
        }
        provideSamplesSubService.changeOrderStatus(vo.getId(), "已发货", "已完成");
        return AjaxResult.success(provideSamplesSubService.getReceiveDetails(vo.getId()));
    }

    @ApiOperation(value = "保存试用反馈")
    @PostMapping("/feedback")
    public AjaxResult saveFeedbackContent(@RequestBody ProvideSamplesSub samplesSub) {
        if(!this.provideSamplesSubService.saveFeedbackContent(samplesSub)){
            return AjaxResult.error("保存失败！");
        }
        return AjaxResult.success("保存成功！");
    }

    @ApiOperation(value = "获取未提交反馈的记录")
    @PostMapping("/getNoFeedback")
    public AjaxResult getNoFeedback(@RequestBody ProvideSamplesSub samplesSub) {
        return AjaxResult.success("获取未提交反馈的记录成功！", this.provideSamplesSubService.getNoFeedback(samplesSub));
    }

    @ApiOperation(value = "评级")
    @PostMapping("/rating")
    public AjaxResult rating(@RequestBody ProvideSamplesSub samplesSub) {
        if(!this.provideSamplesSubService.rating(samplesSub)){
            return AjaxResult.error("评级失败！");
        }
        return AjaxResult.success("评级成功！", this.provideSamplesSubService.getById(samplesSub.getId()));
    }

    @ApiOperation(value = "根据订单id获取领取样品记录详情")
    @GetMapping("/info/byOrderId/{orderId}")
    public AjaxResult infoByOrderId(@PathVariable String orderId){
        if (StrUtil.isBlank(orderId)) {
            return AjaxResult.error("获取不到orderId！");
        }
        LambdaQueryWrapper<ProvideSamplesSub> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProvideSamplesSub::getOrderId, orderId);
        return AjaxResult.success("获取领取样品记录详情成功！", provideSamplesSubService.getOne(wrapper));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public TableDataInfo list(ProvideSamplesSubVo vo) {
        startPage();
        List<ProvideSamplesSub> list = provideSamplesSubService.findListByVo(vo);
        return getDataTable(list);
    }

    @ApiOperation("导出样品领取列表")
    @PostMapping("/list/export")
    public void exportList(@RequestBody ProvideSamplesSubVo vo, HttpServletResponse response) {
        List<ProvideSamplesSub> provideSamplesSubs = provideSamplesSubService.findListByVo(vo);
        List<ProvideSamplesSubDTOFactory.ExportDTO> exportDTOList = provideSamplesSubs
                .stream()
                .map(item -> provideSamplesSubDTOFactory.toExportDTO.apply(item))
                .collect(Collectors.toList());
        ExcelUtil<ProvideSamplesSubDTOFactory.ExportDTO> util = new ExcelUtil<>(ProvideSamplesSubDTOFactory.ExportDTO.class);
        util.exportExcel(response, exportDTOList, "样品领取记录");
    }

    @ApiOperation(value = "修改收货信息")
    @PostMapping("/modifyReceivingInfo")
    public AjaxResult modifyReceivingInfo(@RequestBody ProvideSamplesSub provideSamplesSub) {
        return this.provideSamplesSubService.modifyReceivingInfo(provideSamplesSub);
    }

}
